package egs.strings;



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;

import java.io.FileReader;
import java.io.FileWriter;


import java.io.IOException;

 class EmailFilter {
	private String line;
	private FileReader fr;
	private BufferedReader br;
	private FileWriter fw;
	private BufferedWriter bw;
	
	public boolean wirteFiltredEmail(File source, File destination, String filterStr) throws IOException {
		fr = new FileReader(source);
		br = new BufferedReader(fr);
		
		fw = new FileWriter(destination);
		bw = new BufferedWriter(fw);
		
		if(!destination.exists()) {
			destination.createNewFile();
		}	
		
		if(source.exists()){
			while( (line = br.readLine()) != null){
				if(line.contains(filterStr)) {
					//System.out.println(line);
					bw.write(line);
					bw.newLine();
				}
				
			}
			bw.close();
			fw.close();
			
			return true;
		}
		else {
			return false;
		}
	}
	
	
	public void readFileToConsole(File file) throws IOException {
		fr = new FileReader(file);
		br = new BufferedReader(fr);
		System.out.println();
		
		if(file.exists()) {
			System.out.println("reading file : " + file.getName());
			while( (line = br.readLine()) != null){
					System.out.println(line);
			}
		}
	}

}




public class email {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EmailFilter ef = new EmailFilter();
		File source = new File("email.txt");
		File destination = new File("gmail.txt");
		try {
			ef.wirteFiltredEmail(source, destination, "gmail");			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			ef.readFileToConsole(destination);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}
